package com.itzz.spring_zhoneHeng.controller;

import com.itzz.spring_zhoneHeng.base.RestResponse;
import com.itzz.spring_zhoneHeng.dao.ClassificationDao;
import com.itzz.spring_zhoneHeng.pojo.Classification;
import com.itzz.spring_zhoneHeng.service.ClassificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.Oneway;
import java.util.List;


@RestController
@RequestMapping("/classification")
public class ClassificationController {

    @Autowired
    ClassificationDao dao;
    @Autowired
    ClassificationService service;

    //(不需要分页)查询所有类型
    @RequestMapping("/queryAllCategory")
    public RestResponse category(Classification classification){
        List<Classification> classifications = dao.queryAll(classification);
        return RestResponse.ok(classifications);
    }



    //查询所有类型(需要分页)
        @RequestMapping("/queryAll")
        public RestResponse list(Classification classification, Integer pageNum, Integer pageSize){
            RestResponse response = service.queryAll(classification, pageNum, pageSize);
            return response;
        }



    //回显分类
    @RequestMapping("/queryById/{id}")
    public RestResponse queryById(@PathVariable Integer id, Classification classification){
         if(id!=null){
             classification.setId(id);
         }
        List<Classification> classifications = dao.queryAll(classification);

        return RestResponse.ok(classifications);
    }

    //修改
    @RequestMapping("/update")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse update(Classification classification){
        RestResponse response = service.update(classification);
        return response;
    }

    //删除
    @RequestMapping("/delete/{id}")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse delete(@PathVariable Integer id){
        RestResponse response = service.delete(id);
        return response;
    }


    //添加
    @RequestMapping("/add")
    @PreAuthorize("hasRole('ROLE_super')")
    public RestResponse add(Classification classification){
        RestResponse response = service.add(classification);
        return response;
    }



}
